package cn.ekuma.epos.datalogic.define.dao.erp;

import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.datalogic.define.dao.ProductDAO;
import cn.ekuma.epos.db.table.erp.I_OperationProduct;

import com.openbravo.bean.Product;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.KeyBuilder;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import com.openbravo.pos.bean.ProductExt;
import com.openbravo.pos.bean.erp.OperationProductCategoryExt;
import com.openbravo.bean.erp.OperationProduct;
import com.openbravo.bean.erp.OperationProductCategory;

public class OperationProductDAO extends ModifiedLogDAO<OperationProduct> {
	 ProductDAO productDAO;
	
	public OperationProductDAO(I_Session s,ProductDAO productDAO) {
		super(s);
		this.productDAO=productDAO;
	}

	public OperationProductDAO(I_Session s) {
		super(s);
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s, "OPERATIONPRODUCT", new Field[] {
				new Field(I_OperationProduct.ID, Datas.STRING, Formats.STRING),
				new Field(I_OperationProduct.OPERATIONCATEGORIE, Datas.STRING, Formats.STRING,
						OperationProductCategory.class),
				new Field(I_OperationProduct.PRODUCT, Datas.STRING, Formats.STRING,
						Product.class),
				new Field(I_OperationProduct.ATTRIBUTESETINSTANCE_ID, Datas.STRING,
						Formats.STRING),
				new Field(I_OperationProduct.UNITS, Datas.DOUBLE, Formats.DOUBLE),
				new Field(I_OperationProduct.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP)},
				new int[] { 0 });
	}

	@Override
	public OperationProduct readValues(DataRead dr,OperationProduct obj) throws BasicException {
		if(obj==null)
			obj=new OperationProduct();
		obj.setId(dr.getString(1));
		obj.setOperationCategoryId(dr.getString(2));
		obj.setProductId(dr.getString(3));
		if(productDAO!=null)
		  obj.setIncludeProduct(productDAO.find(KeyBuilder.getKey(dr.getString(3))));
		obj.setAttrsetinstId(dr.getString(4));
		obj.setUnits(dr.getDouble(5));
		obj.setLastModified(dr.getTimestamp(6));
		return obj;
	}

	@Override
	public void writeInsertValues(DataWrite dp, OperationProduct obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(2, obj.getOperationCategoryId());
		dp.setString(3,obj.getProductId());
		dp.setString(4,obj.getAttrsetinstId());
		dp.setDouble(5,obj.getUnits());
		dp.setTimestamp(6,obj.getLastModified());
	}

	@Override
	public Class getSuportClass() {
		return OperationProduct.class;
	}

	    @Override
	    public Class transClass(Class in) {
	        if(in==ProductExt.class)
	            return Product.class;
	        if(in==OperationProductCategoryExt.class)
	        	return OperationProductCategory.class;
	        return super.transClass(in);
	    }

 
}
